package com.weiai.wedding.dao.file;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Order;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.weiai.wedding.dao.BaseQueryDao;
import com.weiai.wedding.entity.file.UserFile;
import com.weiai.wedding.util.Pagination;


@Component("userFileExpDao")
public  class UserFileExpDaoImpl implements UserFileExpDao {
	@PersistenceContext 
	private EntityManager em;
	@Autowired
	private BaseQueryDao base;
	
	@Override
	public Pagination<UserFile> findUserFileList(UserFile userFile, Pagination<UserFile> page) {
		CriteriaBuilder builder = em.getCriteriaBuilder(); 
		CriteriaQuery<UserFile> query = builder.createQuery(UserFile.class); 
		Root<UserFile> userFileRoot = query.from(UserFile.class); 
		query.select(userFileRoot);
		List<Predicate> predicateList = new ArrayList<Predicate>(); 
//		Predicate userBatchNumPredicate; 
		
		if(StringUtils.isNotBlank(userFile.getId())){
			predicateList.add(builder.equal(userFileRoot.get("id"),userFile.getId()));
		}
		
		if(StringUtils.isNotBlank(userFile.getUserId())){
			predicateList.add(builder.equal(userFileRoot.get("userId"), userFile.getUserId()));
		}
		
		if(StringUtils.isNotBlank(userFile.getFileName())){
			predicateList.add(builder.equal(userFileRoot.get("fileName"),userFile.getFileName()));
		}
		
		if(StringUtils.isNotBlank(userFile.getFileSuffix())){
			predicateList.add(builder.equal(userFileRoot.get("fileSuffix"),userFile.getFileSuffix()));
		}
		
		if(StringUtils.isNotBlank(userFile.getDomain())){
			predicateList.add(builder.equal(userFileRoot.get("domain"),userFile.getDomain()));
		}
		
		if(StringUtils.isNotBlank(userFile.getDownloadUrl())){
			predicateList.add(builder.equal(userFileRoot.get("downLoadUrl"),userFile.getDownloadUrl()));
		}
		
		if(StringUtils.isNotBlank(userFile.getUploadPath())){
			predicateList.add(builder.equal(userFileRoot.get("uploadPath"),userFile.getUploadPath()));
		}
		
		if(StringUtils.isNotBlank(userFile.getCreatedBy())){
			predicateList.add(builder.equal(userFileRoot.get("createdBy"),userFile.getCreatedBy()));
		}
		
		if(userFile.getCreatedTime()!=null){

			predicateList.add(builder.equal(userFileRoot.get("createdTime"),userFile.getCreatedTime()));
		}
		
		if(userFile.getBeginCreatedTime()!=null){

			predicateList.add(builder.greaterThanOrEqualTo(
					userFileRoot.get("createdTime").as(Date.class),userFile.getBeginCreatedTime()));
		}
		
		if(userFile.getEndCreatedTime()!=null){
			predicateList.add(builder.lessThanOrEqualTo(
					userFileRoot.get("createdTime").as(Date.class),userFile.getEndCreatedTime()));
		}
		
		if(StringUtils.isNotBlank(userFile.getModifyBy())){
			predicateList.add(builder.equal(userFileRoot.get("modifyBy"),userFile.getModifyBy()));
		}
		
		if(userFile.getModifyTime()!=null){

			predicateList.add(builder.equal(userFileRoot.get("modifyTime"),userFile.getModifyTime()));
		}
		
		if(userFile.getBeginModifyTime()!=null){

			predicateList.add(builder.greaterThanOrEqualTo(
					userFileRoot.get("modifyTime").as(Date.class),userFile.getBeginModifyTime()));
		}
		
		if(userFile.getEndModifyTime()!=null){
			predicateList.add(builder.lessThanOrEqualTo(
					userFileRoot.get("modifyTime").as(Date.class),userFile.getEndModifyTime()));
		}
				
		Predicate[] predicates = new Predicate[predicateList.size()]; 
		predicateList.toArray(predicates); 
		query.where(predicates); 
		Order desc = builder.desc(userFileRoot.get("id"));
		query.orderBy(desc);
		Pagination pg = base.getPage(query, page, UserFile.class, predicates);
		return pg; 

	}
	
}
